<?php
namespace catchAdmin\apidoctor\controller;

use catchAdmin\registration\service\TaskService;
use catcher\Code;
use catcher\exceptions\FailedException;
use think\exception\FileException;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use think\exception\ValidateException;
use app\common\model\Followtask as TaskModel;
use catchAdmin\apidoctor\validate\SfglValidate;
use app\common\model\PushMessage as PushMessageModel;
use app\common\model\PropagandaEducationHistory as PropagandaEducationHistoryModel;
use app\common\model\FollowHistory as FHModel;
use app\common\model\Follow as FollowModel;

class Sfgl extends CatchController
{ 
    /**
     * 随访表单
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function get_sfgl_sfbd(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(SfglValidate::class)
                ->scene('get_sfgl_sfbd')
                ->check($req);
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
        return CatchResponse::paginate((new FHModel())->getRecord($req['patientId']));
    }

    /**
     * 健康宣教
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function get_sfgl_jkxj(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(SfglValidate::class)
                ->scene('get_sfgl_jkxj')
                ->check($req);
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
        return CatchResponse::success((new PropagandaEducationHistoryModel)->getMyEd($req['patientId']));
    }

    /**
     * 消息列表
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function get_ysd_message_list(CatchRequest $request): \think\response\Json
    {
        $where[] = ['creator_id', '=', request()->userInfo['id']];
        $where[] = ['category', '=', 'Doctor'];
        return CatchResponse::paginate((new PushMessageModel())->getLists($where));
    }

    /**
     * 统计未读消息数
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getYsdUnreadMessageNum(CatchRequest $request): \think\response\Json
    {
        $where[] = ['creator_id', '=', request()->userInfo['id']];
        $where[] = ['category', '=', 'Doctor'];
        $where[] = ['isview', '=', 0];
        $num = (new PushMessageModel())->getCount($where);
        return CatchResponse::success(['unread' => $num]);
    }

    /**
     * 清除消息
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function cleanYsdUnreadMessage(CatchRequest $request): \think\response\Json
    {
        $id = $request->param('id');
        if (!isset($id) || empty($id)) {
            throw new FileException("id不能为空");
        }
        return CatchResponse::success((new PushMessageModel())->delData($id));
    }

    /**
     * 随访任务-门诊/病区随访
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getYsdFollowTask(CatchRequest $request): \think\response\Json
    {
        $where[] = ['follow_staff.user_id', '=', request()->userInfo['id']];
        $field = "id,taskname,endtime,status";
        return CatchResponse::paginate((new TaskModel())->myFollowData($where, $field));
    }

    /**
     * 随访任务-门诊/病区随访-详情信息
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getYsdFollowTaskDetailsHead(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        if (!isset($req['id']) || empty($req['id'])) {
            throw new FailedException("id参数不能为空");
        }
        $list = (new TaskService())->getTaskAggregation($req['id']);
        return CatchResponse::success($list['head']??[]);
    }

    /**
     * 随访任务-门诊/病区随访-详情
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getYsdFollowTaskDetails(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::paginate((new FollowModel())->getLists());
    }

    /**
     * 阅读消息
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function readYsdMessage(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        if (!isset($req['id']) || empty($req['id'])) {
            throw new FailedException('id不能为空');
        }
        $data = ['isview' => 1];
        return CatchResponse::success((new PushMessageModel())->saveData($req['id'], $data));
    }

    /**
     * 推送渠道问卷
     *
     * @time 2022年09月21日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function followYsdPush(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success((new TaskService())->pushQuestionnaire($request->param()));
    }

    /**
     * 宣教计划
     *
     * @time 2023年08月11日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function edYsdPlanList(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        $ret = (new PropagandaEducationHistoryModel())->getList($req);
        $result = array_merge($ret, ["code" => Code::SUCCESS, "message" => "success"]);
        return json($result);
    }

    /**
     * 宣教计划详情
     *
     * @time 2023年08月11日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function edYsdPlanDetails(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        if (!isset($req['id']) || empty($req['id']))
        {
            throw new FailedException('缺少id参数');
        }

        $ret = (new PropagandaEducationHistoryModel())->getEdhistoryDetails($req);
        $result = array_merge($ret, ["code" => Code::SUCCESS, "message" => "success"]);
        return json($result);
    }
}